<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script src='${pageContext.request.contextPath}/assets/js/jquery-2.0.3.min.js'></script>
<table border="1" width="100%">
		<thead>
 		<tr>
 			<th width='130px'>时间</th><th week='1'>周一</th><th  week='2'>周二</th><th  week='3'>周三</th><th  week='4'>周四</th><th  week='5'>周五</th><th  week='6'>周六</th><th  week='7'>周日</th>
 		</tr>
		</thead>
	 <tbody id="showPlant_tableBody">
	 
	 </tbody>
</table> 
<script type="text/javascript">
(function(){
	var teacherId = "${param.table_id}";
	var userType="${param.userType}"; 
	//加载课表
 	function loadClassPlant(){
		
 		var url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=getTeacherPlantMaster";
		if(userType=="1"){
			 
			url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=getStudentPlantMaster";
		}
 		$.ajax({
			   type: "POST",
			   url: url,
			   async:false,
			   dataType:'json',
			   data: {table_id:teacherId},
			   success: function(msg){  
			      if(msg && msg.list && msg.list[0]!=null){ 
			    	  showMsg(msg);
			      }
			   }
		});
 	};
 	//显示课表
 	function  showMsg(msg){ 
 		var arr = new Array(); 
 		//这个循环是为了去掉重复的时间问题
		 for(var i=0;i<msg.list.length;i++){
			 var b =false;
			 for(var j=0;j<arr.length;j++){
				 if(arr[j].start1 ==msg.list[i].start1 && arr[j].end == msg.list[i].end){
					b=true;
					break;
				 }
			 }
			 if(b==false){
				 arr.push(msg.list[i]);
				// alert(msg.list[i].start1 +"=="+msg.list[i].end);
			 }
		 }  
 		 //按左边的时间，找到对应的课表，显示
 		 for(var i=0;i<arr.length;i++){ 
    		 var tbody =$("#showPlant_tableBody");
    		 var kbit1 = arr[i];
    		 var tr =$('<tr></tr>');
    		 var td0 =$("<td>"+ kbit1.start1+"--"+ kbit1.end+"</td>");
			 var td1 =$("<td week='1'></td>");
			 var td2 =$("<td week='2'></td>");
			 var td3 =$("<td week='3'></td>");
			 var td4 =$("<td week='4'></td>");
			 var td5 =$("<td week='5'></td>");
			 var td6 =$("<td week='6'></td>");
			 var td7 =$("<td week='7'></td>");
    		 for(var j=0;j<msg.list.length;j++){  
    			 var kbit2 = msg.list[j];
    			 if(kbit2.start1 == kbit1.start1 && kbit2.end == kbit1.end){
    				 var kct="";
    				  if(kbit2.string4.length>0){ 
    					  kct='<font color="black">(['+kbit2.classTimeShow+']'+kbit2.string4+')</font>';
    				  }
    				  if(kbit2.week ==1){
    					  td1.html(kbit2.sname +kct);
    					  td1.attr("start1",kbit1.start1);
    					  td1.attr("end",kbit1.end);
    					  td1.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td1.attr("table_id",kbit2.table_id);
    					  td1.attr("week","1");
    					  td1.data("ml",msg.list[j]);
    				  } 
    				  if(kbit2.week ==2){
    					  td2.html(kbit2.sname+kct);
    					  td2.attr("start1",kbit1.start1);
    					  td2.attr("end",kbit1.end);
    					  td2.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td2.data("ml",msg.list[j]);
    					  td2.attr("week","2");
    					  td2.attr("table_id",kbit2.table_id);
    				  } 
    				  if(kbit2.week ==3){
    					  td3.html(kbit2.sname+kct);
    					  td3.attr("start1",kbit1.start1);
    					  td3.attr("end",kbit1.end);
    					  td3.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td3.data("ml",msg.list[j]);
    					  td3.attr("week","3");
    					  td3.attr("table_id",kbit2.table_id);
    				  } 
    				  if(kbit2.week ==4){
    					  td4.html(kbit2.sname+kct);
    					  td4.attr("start1",kbit1.start1);
    					  td4.attr("end",kbit1.end);
    					  td4.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td4.data("ml",msg.list[j]);
    					  td4.attr("week","4");
    					  td4.attr("table_id",kbit2.table_id);
    				  } 
    				  if(kbit2.week ==5){
    					  td5.html(kbit2.sname+kct);
    					  td5.attr("start1",kbit1.start1);
    					  td5.attr("end",kbit1.end);
    					  td5.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td5.data("ml",msg.list[j]);
    					  td5.attr("week","5");
    					  td5.attr("table_id",kbit2.table_id);
    				  } 
    				  if(kbit2.week ==6){
    					  td6.html(kbit2.sname+kct);
    					  td6.attr("start1",kbit1.start1);
    					  td6.attr("end",kbit1.end);
    					  td6.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td6.data("ml",msg.list[j]);
    					  td6.attr("week","6");
    					  td6.attr("table_id",kbit2.table_id);
    				  } 
    				  if(kbit2.week ==7){
    					  td7.html(kbit2.sname+kct);
    					  td7.attr("start1",kbit1.start1);
    					  td7.attr("end",kbit1.end);
    					  td7.attr("classId",kbit1.string1);//因为后台查询的时候把班的ID放在这里的
    					  td7.data("ml",msg.list[j]);
    					  td7.attr("week","7");
    					  td7.attr("table_id",kbit2.table_id);
    				  }  
    			 } 
    		 }
    		 tr.append(td0);tr.append(td1); tr.append(td2); tr.append(td3); tr.append(td4); tr.append(td5); tr.append(td6); tr.append(td7);
   		 	 tbody.append(tr); 
    	 }  
		 if("${sessionScope.sysUser.userType}"==1){
			 return;
		 }
		 
 		 //增加td的事件和现在自己的课程做关联
 		 $("#showPlant_tableBody").find("td").click(function(){
 			var  classid = $(this).attr("classId");			
 			if(classid){
							var object = new Object();
							var obj =$(this);
							object.start1=obj.attr("start1");
							object.end=obj.attr("end");
							object.classId =obj.attr("classId");
							object.table_id =obj.attr("table_id");
							object.week =obj.attr("week");
							
							var text =$(this).data("ml").sname;
							var showtext = "周"+object.week+"--"+object.start1+"--"+object.end+"("+text+")";
							if($("#selectKebiao").length>0){
								$("#selectKebiao").data("class",object);
								$("#selectKebiao").parent().prev().text(showtext);
							}
							$("#learnPlantAdd_ClassWindow").dialog('destroy'); 
							$("#learnPlantAdd_ClassWindow").html("");
							//updateMasterValue(obj,cdata,object);
 			}
 			
 		 });
 	};
 	
 	 
 	//更新模板中的当前所在上课的课
 	function updateMasterValue(obj,data,master){ 
 		var url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=updatePlantMasterValue";
 		$.ajax({
			   type: "POST",
			   url: url,
			   async:false,
			   dataType:'json',
			   data:{lp:obj2str(data),ml:obj2str(master)},
			   success: function(msg){  
			        if(msg.table_id){
			        	alert("关联课程成功");
			        	$(obj).append('<font color="black">(['+msg.classTimeShow+']'+data.pName+')</font>');
			        }else{
			        	alert("关联课程失败");
			        }
			   }
		});
 	};
	$(function(){
		if(teacherId!=""){
			loadClassPlant();
		}
		if("${sessionScope.sysUser.userType}"=="2"||"${sessionScope.sysUser.userType}"=="5" ){
			$("#showRecett").show();
		}
	});
	//删除课模板中，与学期计划的关联
	$("#resctLearnPlant_PlantWeek").click(function(){
		var r=confirm("您确认要重置数据吗？");
		if (r==false){return}; 
		var url ="${pageContext.request.contextPath}/edu/sys_ClassPlantAction.do?method=updateMasterWeekValue";
		$.ajax({
		   type: "POST",
		   url: url,
		   data: {table_id:'${sessionScope.sysUser.table_id}'},
		   success: function(msg){ 
			  if(msg.code==1){
				 if(teacherId!=""){
					loadClassPlant();
				}
			  }
		   }
		}); 
	}); 
	
})();
</script>